package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.controller.GoodsController.GoodsDetail.SubGoodsResponse
import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.entity.Goods
import com.zxy.supplier_system.server.entity.GoodsUnit
import com.zxy.supplier_system.server.entity.SubGoods
import com.zxy.supplier_system.server.repository.QueryGoodsUnitResponse
import com.zxy.supplier_system.server.service.ProcurementReturnService
import com.zxy.supplier_system.server.utils.LongIdAndName
import com.zxy.supplier_system.server.utils.toLongIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal

@RestController
@RequestMapping("procurementSuppliers/returnOrders/{orderId}/items")
class ProcurementReturnsItemController(
    private val procurementReturnService: ProcurementReturnService
) {

    data class AddProcurementReturnsItemRequest(
        val goodsId: Int,
        val amount: Int,
        val subGoodsId: Int?,
        val note: String?,
        val price: BigDecimal = BigDecimal.ZERO,
    )

    @HasAnyAuthority(Authority.采购_退货_修改)
    @PostMapping
    fun addItem(
        @PathVariable orderId: Int,
        @RequestBody addProcurementReturnsItemRequest: AddProcurementReturnsItemRequest
    ) {
        this.procurementReturnService.addItem(orderId, addProcurementReturnsItemRequest)
    }

    @HasAnyAuthority(Authority.采购_退货_修改)
    @DeleteMapping("{goodsId}")
    fun deleteItem(@PathVariable goodsId: Int, @PathVariable orderId: Int) {
        this.procurementReturnService.deleteItem(orderId, goodsId)
    }

    @HasAnyAuthority(Authority.采购_退货_修改)
    @DeleteMapping("{goodsId}/sub/{subGoodsId}")
    fun deleteItem(@PathVariable goodsId: Int, @PathVariable orderId: Int, @PathVariable subGoodsId: Int) {
        this.procurementReturnService.deleteItem(orderId, goodsId, subGoodsId)
    }

    @Transactional
    class UpdateItemAmountRequest(
        val amount: Int,
    )

    @HasAnyAuthority(Authority.采购_退货_修改)
    @PatchMapping("{goodsId}/amount")
    fun updateItemAmount(
        @PathVariable goodsId: Int,
        @PathVariable orderId: Int,
        @RequestBody updateItemAmountRequest: UpdateItemAmountRequest
    ) {
        return this.procurementReturnService.updateItemAmount(goodsId, orderId, updateItemAmountRequest)
    }

    @HasAnyAuthority(Authority.采购_退货_修改)
    @PatchMapping("{goodsId}/sub/{subGoodsId}/amount")
    fun updateItemAmount(
        @PathVariable goodsId: Int,
        @PathVariable subGoodsId: Int,
        @PathVariable orderId: Int,
        @RequestBody updateItemAmountRequest: UpdateItemAmountRequest
    ) {
        return this.procurementReturnService.updateItemAmount(goodsId, orderId, subGoodsId, updateItemAmountRequest)
    }

    @HasAnyAuthority(Authority.采购_退货_修改)
    @PatchMapping("{goodsId}/sub/{subGoodsId}/note")
    fun updateItemNote(
        @PathVariable goodsId: Int,
        @PathVariable orderId: Int,
        @RequestBody note: String,
        @PathVariable subGoodsId: Int
    ) {
        return this.procurementReturnService.updateItemNote(goodsId, orderId, note, subGoodsId)
    }

    @HasAnyAuthority(Authority.采购_退货_修改)
    @PatchMapping("{goodsId}/note")
    fun updateItemNote(
        @PathVariable goodsId: Int,
        @PathVariable orderId: Int,
        @RequestBody note: String,
    ) {
        return this.procurementReturnService.updateItemNote(goodsId, orderId, note)
    }

    @HasAnyAuthority(Authority.采购_退货_修改)
    @PatchMapping("{goodsId}/price")
    fun updateItemPrice(
        @PathVariable goodsId: Int,
        @PathVariable orderId: Int,
        @RequestBody price: BigDecimal,
    ) {
        return this.procurementReturnService.updateItemPrice(goodsId, orderId, price)
    }

    @HasAnyAuthority(Authority.采购_退货_修改)
    @PatchMapping("{goodsId}/sub/{subGoodsId}/price")
    fun updateItemPrice(
        @PathVariable goodsId: Int,
        @PathVariable subGoodsId: Int,
        @PathVariable orderId: Int,
        @RequestBody price: BigDecimal,
    ) {
        return this.procurementReturnService.updateItemPrice(goodsId, subGoodsId, orderId, price)
    }

    @HasAuthority(Authority.采购_退货_查询)
    @GetMapping
    fun queryOrderItemsInOrder(@PathVariable orderId: Int, pageable: Pageable): Page<OrderItemInOrderSummary> {
        return this.procurementReturnService.queryOrderItemsInOrder(orderId, pageable)
    }

    @HasAnyAuthority(Authority.采购_退货_创建)
    @GetMapping("{goodsId}/exists")
    fun exists(@PathVariable goodsId: Int, @PathVariable orderId: Int): Boolean {
        return this.procurementReturnService.exists(orderId, goodsId)
    }

    @HasAnyAuthority(Authority.采购_退货_创建)
    @GetMapping("barcodes/{barcode}/exists")
    fun exists(@PathVariable barcode: String, @PathVariable orderId: Int): Boolean {
        return this.procurementReturnService.exists(orderId, barcode)
    }

    data class OrderItemInOrderSummary(
        val goodsId: Int,
        val name: String,
        val barcode: String,
        val amountInOrder: Int,
        val inStore: Int,
        val boxSpecification: String?,
        val units: List<QueryGoodsUnitResponse>,
        val unit: LongIdAndName?,
        val subGoods: SubGoodsResponse? = null,
        val price:BigDecimal,
    ) {
        companion object {
            fun fromGoodsAndAmountAndUnit(
                goods: Goods, amount: Int, goodsUnit: GoodsUnit?,
                subGoods: SubGoods? = null, returnPrice: BigDecimal = BigDecimal.ZERO
            ): OrderItemInOrderSummary {
                return OrderItemInOrderSummary(
                    goods.id,
                    goods.name,
                    goods.barcode,
                    amount,
                    goods.amount,
                    goods.boxSpecification,
                    goods.units.map(QueryGoodsUnitResponse::formGoodsUnit),
                    goodsUnit?.toLongIdAndName(),
                    subGoods?.let(SubGoodsResponse::fromSubGoods),
                    returnPrice
                )
            }
        }
    }

    @HasAnyAuthority(Authority.采购_退货_创建)
    @PostMapping("barcodes/{barcode}")
    fun addItem(@PathVariable barcode: String, @PathVariable orderId: Int): OrderItemInOrderSummary {
        return this.procurementReturnService.addItem(orderId, barcode)
    }

    @HasAnyAuthority(Authority.采购_退货_创建)
    @PostMapping("goods/{goodsId}")
    fun addItem(@PathVariable goodsId: Int, @PathVariable orderId: Int): OrderItemInOrderSummary {
        return this.procurementReturnService.addItem(orderId, goodsId)
    }

    data class AddProcurementReturnItemKey(
        val goodsId: Int,
        val subGoodsId: Int?,
    )

    @HasAnyAuthority(Authority.采购_退货_修改, Authority.采购_退货_修改)
    @PostMapping("batchAdd")
    fun batchAddItems(
        @PathVariable orderId: Int,
        @RequestBody itemKeys: MutableList<AddProcurementReturnItemKey>
    ): List<OrderItemInOrderSummary> {
        return this.procurementReturnService.batchAddItems(orderId, itemKeys)
    }

}